package com.quzitech.efest.view.chart;

import java.util.ArrayList;

import com.quzitech.efest.utils.Myutil;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.WindowManager;

/**
 * 线框�??
 * 
 * @author maijuntian
 * 
 */
public class LineChartYDate extends View {

	private ArrayList<ArrayList<Integer>> dataLists;
	private int verticalGridNum;
	private int verticalGridNumMin;
	private final int bottomLineLength = Myutil.sp2px(getContext(), 20);
	private int mViewHeight;
	private int mViewWidth;
	private Paint paint_date;
	int screenHeight, screenWidth;
	private int whiteLineColor = 0xffffffff; // 白色
	private final int BOTTOM_TEXT_COLOR = Color.parseColor("#9B9A9B");
	private float left_blank = 0.3f;

	public LineChartYDate(Context context,
			ArrayList<ArrayList<Integer>> dataLists1) {
		super(context);
		initScreenSize(getContext());
		init();
		this.setDataList(dataLists1);

	}

	private void initScreenSize(Context context) {
		// ��ô��ڹ���? ��
		WindowManager manager = (WindowManager) context
				.getSystemService(Context.WINDOW_SERVICE);
		// ����һ����ʾ���Զ���
		DisplayMetrics dm = new DisplayMetrics();
		// ͨ��manager��һЩֵ ��ò�������?(��װ)��dm��
		manager.getDefaultDisplay().getMetrics(dm);
		// �����Ļ��?
		screenWidth = dm.widthPixels;
		// �����Ļ��?
		screenHeight = dm.heightPixels;

	}

	public void init() {
		setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
				LayoutParams.MATCH_PARENT));

		paint_date = new Paint();
		paint_date.setAntiAlias(true);
		paint_date.setColor(BOTTOM_TEXT_COLOR);
		paint_date.setTextSize(Myutil.sp2px(getContext(), 13));

	}

	public void setDataList(ArrayList<ArrayList<Integer>> dataLists) {

		this.dataLists = dataLists;
		getMinandMax();
		postInvalidate();
		// init();
	}

	protected void onDraw(Canvas c) {
		drawYLine(c);
		drawYData(c);
	}

	public void drawYLine(Canvas canvas) {
		Log.v("test", "i have into the y" + " height" + screenHeight + "width"
				+ screenWidth);
		int i = mViewHeight - bottomLineLength;
		canvas.drawLine((getWidth() * left_blank), (float) (i * 0),
				(float) getWidth(), (float) (i * 0), paint_date);
		canvas.drawLine((getWidth() * left_blank),
				(float) (i * (double) 1 / 3), (float) getWidth(), (float) (i
						* (double) 1 / 3), paint_date);
		canvas.drawLine((getWidth() * left_blank),
				(float) (i * (double) 2 / 3), (float) getWidth(), (float) (i
						* (double) 2 / 3), paint_date);
		canvas.drawLine((getWidth() * left_blank), (float) (i * 1),
				(float) getWidth(), (float) (i * 1), paint_date);
	}

	private void drawYData(Canvas canvas) {
		int i = mViewHeight - bottomLineLength;
		int perLineInterval = (verticalGridNum - verticalGridNumMin) / 5;
		canvas.drawText(verticalGridNumMin + "", (getWidth() * left_blank),
				(float) (i * 1), paint_date);

		canvas.drawText(verticalGridNumMin + perLineInterval * 2 + "",
				(getWidth() * left_blank), (float) (i * 0), paint_date);
		canvas.drawText(verticalGridNumMin + perLineInterval * 3 + "",
				(getWidth() * left_blank), (float) (i * (double) 2 / 3),
				paint_date);
		canvas.drawText(verticalGridNumMin + perLineInterval * 4 + "",
				(getWidth() * left_blank), (float) (i * (double) 1 / 3),
				paint_date);

	}

	private void getMinandMax() {
		verticalGridNum = 0;
		verticalGridNumMin = 10000;
		if (dataLists != null && !dataLists.isEmpty()) {
			for (ArrayList<Integer> list : dataLists) {
				for (Integer integer : list) {
					if (verticalGridNum < (integer + 1)) {
						verticalGridNum = intUp(integer);
					}
					if (verticalGridNumMin > (integer)) {
						verticalGridNumMin = intDown(integer);
					}
				}
			}
		}

	}

	private int intDown(int intStr) {
		String s = String.valueOf(intStr);
		int[] intArray = new int[s.length()];
		for (int i = 0; i < s.length(); i++) {
			Character ch = s.charAt(i);
			intArray[i] = Integer.parseInt(ch.toString());
		}
		if (s.length() <= 2) {
			double d = (intArray[0] + 1) * (Math.pow(10, s.length() - 1));
			return 0;
		} else if (s.length() == 3) {
			if ((intArray[0] > 1)) {
				return (int) ((intArray[0] - 1) * (Math.pow(10, s.length() - 1)));
			} else {
				return 100;
			}

		} else {
			double d = (intArray[0]) * (Math.pow(10, s.length() - 1))
					+ (intArray[1] - 1) * (Math.pow(10, s.length() - 2));
			return (int) d;
		}

	}

	private int intUp(int intStr) {
		String s = String.valueOf(intStr);
		int[] intArray = new int[s.length()];
		for (int i = 0; i < s.length(); i++) {
			Character ch = s.charAt(i);
			intArray[i] = Integer.parseInt(ch.toString());
		}
		if (s.length() <= 3) {
			return (int) ((intArray[0] + 2) * (Math.pow(10, s.length() - 1)));
		} else {
			return (int) ((intArray[0]) * (Math.pow(10, s.length() - 1)) + (intArray[1] + 2)
					* (Math.pow(10, s.length() - 2)));

		}

	}

	private int measureWidth(int measureSpec) {

		int preferred = 0;
		return getMeasurement(measureSpec, preferred);
	}

	private int measureHeight(int measureSpec) {
		int preferred = 0;
		return getMeasurement(measureSpec, preferred);
	}

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

		mViewHeight = measureHeight(heightMeasureSpec);
		mViewWidth = measureWidth(widthMeasureSpec);
		Log.v("test", "i have into the y" + "heightMeasureSpec---->"
				+ mViewHeight + "widthMeasureSpec---->" + mViewWidth);
		setMeasuredDimension(mViewWidth, mViewHeight);
	}

	private int getMeasurement(int measureSpec, int preferred) {
		int specSize = MeasureSpec.getSize(measureSpec);
		int measurement;
		switch (MeasureSpec.getMode(measureSpec)) {
		case MeasureSpec.EXACTLY:
			measurement = specSize;
			break;
		case MeasureSpec.AT_MOST:
			measurement = Math.max(preferred, specSize);
			break;
		default:
			measurement = preferred;
			break;
		}
		return measurement;
	}

}
